package com.jungle.sp.daily.util;


import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.jungle.sp.daily.dto.CommonDTO;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Jungle
 * @create 2024-05-17 16:31
 */
public class Tool {
    /**
     * 生成签名
     * @param jsonObject 参数列表
     * @param appSecret 密钥
     * @return 签名字符串
     */
    public static String sign(JSONObject jsonObject, String appSecret) {
        JSONObject sortObject = JsonSortUtils.startSort(jsonObject);
        List<String> paramList = new ArrayList<>(sortObject.keySet());
        StringBuilder signsb = new StringBuilder();
        for (String str : paramList) {
            if (sortObject.get(str) == null || sortObject.get(str).equals("")) {
                continue;
            }
            Object obj = sortObject.get(str);
            //判断对象是否是对象
            if(obj.getClass().isPrimitive() || obj instanceof String){
                //基本数据类型 || 字符串
                signsb.append("&").append(str).append("=").append(obj);
            }else {
                signsb.append("&").append(str).append("=").append(JSON.toJSONString(obj));
            }
        }
        signsb.append("&appSecret=").append(appSecret);
        System.out.println("生成签名："+signsb.substring(1));
        return DigestUtils.md5DigestAsHex(signsb.substring(1).getBytes()).toUpperCase();
    }

    public static void main1(String[] args) {
        // String paramJSON = "{\"shippingDate\":\"2024-05-11 11:33:15\",\"pieces\":1,\"volumeWeight\":1,\"shippingSiteCode\":\"H004\",\"shippingSiteName\":\"华新二级\",\"shiftCode\":\"007002\",\"shiftName\":\"晚班\",\"balanceWeight\":100,\"cleanFreight\":10,\"theReceiptId\":\"0\",\"expressCode\":\"023001\",\"expressType\":\"珠三角件\",\"logisticsModeCode\":\"032001\",\"logisticsMode\":\"汽运\",\"itemCategoryCode\":\"004001\",\"itemCategoryName\":\"标准快递\",\"serviceModeCode\":\"017001\",\"serviceMode\":\"派送\",\"totalFreight\":\"10.00\",\"payMethodCode\":\"036002\",\"payMethodName\":\"到付\",\"pickerName\":\"华新2级员工\",\"pickerNo\":\"000044\",\"waybillNo\":\"11111\",\"senderTel\":\"18949555420\",\"sender\":\"张三\",\"categoryName\":\"材料\",\"senderAddress\":\"软件园中路\",\"bournCode\":\"S002A\",\"bournName\":\"深圳南山目的地\",\"destSiteName\":\"深圳南山\",\"destSiteCode\":\"S002\",\"theReceiptNo\":null,\"freshBarCode\":null,\"receiver\":\"李四\",\"receiverTel\":\"18949555420\",\"receiverAddress\":\"科技中二路\",\"senderProvince\":\"河北省\",\"senderCity\":\"唐山市\",\"senderTown\":\"路南区\",\"receiverProvince\":\"吉林省\",\"receiverCity\":\"白山市\",\"receiverTown\":\"长白朝鲜族自治县\",\"senderCountry\":\"中国\",\"receiverCountry\":\"中国\"}";
        // String paramJSON = "80000000456";
        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setParams("80000000456");
        commonDTO.setTimestamp(String.valueOf(new Date().getTime()));
        commonDTO.setRequestId(IdUtil.fastUUID());
        String jsonString = JSON.toJSONString(commonDTO);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        String sign = sign(jsonObject, "MSD#2022#test");
        System.out.println("签名==》"+sign);
        System.out.println("时间戳==》"+commonDTO.getTimestamp());
        System.out.println("请求id==》"+commonDTO.getRequestId());
    }

    public static void main(String[] args) {
        System.out.println("00000000000000000");
        CommonDTO commonDTO = new CommonDTO();
        commonDTO.setParams("80000000456");
        commonDTO.setTimestamp(String.valueOf(new Date().getTime()));
        commonDTO.setRequestId(IdUtil.fastUUID());
        String jsonString = JSON.toJSONString(commonDTO);
        JSONObject jsonObject = JSON.parseObject(jsonString);
        String sign = sign(jsonObject, "MSD#2022#test");
        System.out.println("签名==》"+sign);
        System.out.println("时间戳==》"+commonDTO.getTimestamp());
        System.out.println("请求id==》"+commonDTO.getRequestId());

    }
}
